//
//  LXRequest.swift
//  LXTXShop
//
//  Created by apple on 2018/4/10.
//  Copyright © 2018年 LX. All rights reserved.
//

import UIKit
import SwiftyJSON

class LXRequest: NSObject {
    fileprivate func post (_ url:String , _ params:Dictionary< String, String>?,success:@escaping ((JSON)->()),failure:@escaping ((String)->()) ){
        LXNetworkTool().requestUrl(url,params: params,success: {
            (res) in
            success(res)
        },failure: {
            (f) in
            failure(f)
        })
    }
    
    /// 首页
    ///
    /// - Parameters:
    ///   - params: nil
    ///   - success: 成功回调
    ///   - failure: 失败回调
    func indexNew(_ params:Dictionary< String, String>?  ,success:@escaping ((JSON)->()),failure:@escaping ((String)->())) {
        
        self.post("", params, success: { (data) in
            success(data)
        }) { (errorStr) in
            failure(errorStr)
        }
    }
    
    
    /// 登录
    ///
    /// - Parameters:
    ///   - params: NSMutableDictionary
    ///   手机号:member_mobile,
    ///   登录密码:member_passwd
    ///   - success: 成功回调
    ///   - failure: 失败回调
    func login(_ params:Dictionary< String, String>?  ,success:@escaping ((JSON)->()),failure:@escaping ((String)->())) {
        
        self.post("", params, success: { (data) in
            success(data)
        }) { (errorStr) in
            failure(errorStr)
        }
    }
    
    /// 退出登录
    ///
    /// - Parameters:
    ///   - params: key
    ///   - success: 成功回调
    ///   - failure: 失败回调
    func logout(_ params:Dictionary< String, String>?  ,success:@escaping ((JSON)->()),failure:@escaping ((String)->())) {
        
        self.post("", params, success: { (data) in
            success(data)
        }) { (errorStr) in
            failure(errorStr)
        }
    }
    
    /// 发送验证码
    ///
    /// - Parameters:
    ///   - params: mobile(手机号)
    ///   - params: messageType
    ///   - success: 成功回调
    ///   - failure: 失败回调
    func sendMessage(_ params:Dictionary< String, String>?  ,success:@escaping ((JSON)->()),failure:@escaping ((String)->())) {
        
        self.post("", params, success: { (data) in
            success(data)
        }) { (errorStr) in
            failure(errorStr)
        }
    }
    
    /// 检查验证码
    ///
    /// - Parameters:
    ///   - params: mobile(手机号)
    ///   - params: messageType
    ///   - success: 成功回调
    ///   - failure: 失败回调
    func checkCodeCheck(_ params:Dictionary< String, String>?  ,success:@escaping ((JSON)->()),failure:@escaping ((String)->())) {
        
        self.post("", params, success: { (data) in
            success(data)
        }) { (errorStr) in
            failure(errorStr)
        }
    }
    
    /// 注册
    ///
    /// - Parameters:
    ///   - params: member_mobile(手机号)
    ///   - params: password
    ///   - params: check_code
    ///   - params: user_friend
    ///   - success: 成功回调
    ///   - failure: 失败回调
    func memberRegister(_ params:Dictionary< String, String>?  ,success:@escaping ((JSON)->()),failure:@escaping ((String)->())) {
        
        self.post("", params, success: { (data) in
            success(data)
        }) { (errorStr) in
            failure(errorStr)
        }
    }
    
    /// 图片上传
    ///
    /// - Parameters:
    ///   - success: 成功回调
    ///   - failure: 失败回调
    
    /// 2.11照片信息上传
    
    func startMultiPartUploadTaskWithURL(_ url: String,_ parameters: Dictionary< String, String>?,data: [Data],success: @escaping (_ response:(JSON)) -> (),failedBlock: @escaping (_ error: Error) -> ()){
        LXNetworkTool().upLoadImage(urlString: url, params: parameters, data: data, success: { (data) in
            success(data)
        },failture: { (error) in
            failedBlock(error)
        })
    }
}
